package com.google.code.timetrail.gui;

import javax.swing.JPanel;

import com.google.code.timetrail.backend.Control;
import com.google.code.timetrail.presenter.SkillFrameBackend;

import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JButton;

import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SkillFrame extends JPanel{

    private static final long serialVersionUID = 9165456944300202218L;
    private Control myGameControl;
    private JPanel myCD;
    private SkillFrameBackend skillBackend;

    public SkillFrame(Control gameControl, JPanel cd){
        myGameControl = gameControl;
        myCD = cd;
        skillBackend = new SkillFrameBackend(myGameControl);
        final int MIN_SKILL_LEVEL = skillBackend.getMinSkillLevel();
        final int MAX_SKILL_LEVEL = skillBackend.getMaxSkillLevel();
        setLayout(null);
        
        JLabel swimSkillLabel = new JLabel(skillBackend.getSwimLabel());
        swimSkillLabel.setBounds(49, 37, 59, 14);
        add(swimSkillLabel);
        
        JLabel fitnessSkillLabel = new JLabel(skillBackend.getFitnessLabel());
        fitnessSkillLabel.setBounds(49, 85, 59, 14);
        add(fitnessSkillLabel);
        
        JLabel bargainSkillLabel = new JLabel(skillBackend.getBargainLabel());
        bargainSkillLabel.setBounds(49, 134, 59, 14);
        add(bargainSkillLabel);
        
        JLabel immunitySkillLabel = new JLabel(skillBackend.getImmunityLabel());
        immunitySkillLabel.setBounds(49, 173, 59, 14);
        add(immunitySkillLabel);
        
        JProgressBar swimSkillProgress = new JProgressBar(MIN_SKILL_LEVEL, MAX_SKILL_LEVEL);
        swimSkillProgress.setValue(skillBackend.getSwimSkill());
        swimSkillProgress.setBounds(118, 37, 146, 14);
        add(swimSkillProgress);
        
        JProgressBar fitnessSkillProgress = new JProgressBar(MIN_SKILL_LEVEL, MAX_SKILL_LEVEL);
        fitnessSkillProgress.setValue(skillBackend.getFitnessSkill());
        fitnessSkillProgress.setBounds(118, 85, 146, 14);
        add(fitnessSkillProgress);
        
        JProgressBar bargainSkillProgress = new JProgressBar(MIN_SKILL_LEVEL, MAX_SKILL_LEVEL);
        bargainSkillProgress.setValue(skillBackend.getBargainSkill());
        bargainSkillProgress.setBounds(118, 134, 146, 14);
        add(bargainSkillProgress);
        
        JProgressBar immunitySkillProgress = new JProgressBar(MIN_SKILL_LEVEL, MAX_SKILL_LEVEL);
        immunitySkillProgress.setValue(skillBackend.getImmunitySkill());
        immunitySkillProgress.setBounds(118, 173, 146, 14);
        add(immunitySkillProgress);
        
        JButton backButton = new JButton(skillBackend.getBackButtonText());
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                myCD.add(new InventoryFrame(myGameControl, myCD), "skills");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        backButton.setBounds(321, 248, 89, 23);
        add(backButton);
        
        JButton btnUpgradeImmunity = new JButton("Upgrade");
        btnUpgradeImmunity.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                skillBackend.upgradeImmunity();
                myCD.add(new SkillFrame(myGameControl, myCD), "skills");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnUpgradeImmunity.setBounds(293, 169, 89, 23);
        add(btnUpgradeImmunity);
        
        JButton btnUpgradeBargaining = new JButton("Upgrade");
        btnUpgradeBargaining.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                skillBackend.upgradeBargaining();
                myCD.add(new SkillFrame(myGameControl, myCD), "skills");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnUpgradeBargaining.setBounds(293, 130, 89, 23);
        add(btnUpgradeBargaining);
        
        JButton btnUpgradeFitness = new JButton("Upgrade");
        btnUpgradeFitness.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                skillBackend.upgradeFitness();
                myCD.add(new SkillFrame(myGameControl, myCD), "skills");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnUpgradeFitness.setBounds(293, 81, 89, 23);
        add(btnUpgradeFitness);
        
        JButton btnUpgradeSwimming = new JButton("Upgrade");
        btnUpgradeSwimming.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                skillBackend.upgradeSwimming();
                myCD.add(new SkillFrame(myGameControl, myCD), "skills");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
            }
        });
        btnUpgradeSwimming.setBounds(293, 33, 89, 23);
        add(btnUpgradeSwimming);
        
    }
}
